package com.jfeat.am.module.wegou.api.crud;

import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import com.jfeat.am.module.wegou.services.domain.service.QueryUserIdentityService;
import com.jfeat.am.common.constant.tips.SuccessTip;
import com.jfeat.am.common.constant.tips.Tip;
import com.jfeat.am.module.log.annotation.BusinessLog;

import com.jfeat.am.module.wegou.services.crud.service.UserIdentityService;
import com.jfeat.am.module.wegou.services.persistence.model.UserIdentity;

import org.springframework.web.bind.annotation.RestController;
import com.jfeat.am.common.controller.BaseController;

import javax.annotation.Resource;
import java.util.Date;


/**
 * <p>
 * api
 * </p>
 *
 * @author Code Generator
 * @since 2018-01-29
 */
@Deprecated
@RestController
@RequestMapping("/api/wegou/user-identity")
public class UserIdentityEndpoint extends BaseController {

    @Resource
    UserIdentityService userIdentityService;

    @Resource
    QueryUserIdentityService queryUserIdentityService;

    @PostMapping
    @BusinessLog(name = "UserIdentity", value = "create UserIdentity")
    public Tip createUserIdentity(@RequestBody UserIdentity entity) {
        return SuccessTip.create(userIdentityService.createMaster(entity));
    }

    @GetMapping("/{id}")
    public Tip getUserIdentity(@PathVariable Long id) {
        return SuccessTip.create(userIdentityService.retrieveMaster(id));
    }

    @PutMapping("/{id}")
    @BusinessLog(name = "UserIdentity", value = "update UserIdentity")
    public Tip updateUserIdentity(@PathVariable Long id, @RequestBody UserIdentity entity) {
        entity.setId(id);
        return SuccessTip.create(userIdentityService.updateMaster(entity));
    }

    @DeleteMapping("/{id}")
    @BusinessLog(name = "UserIdentity", value = "delete UserIdentity")
    public Tip deleteUserIdentity(@PathVariable Long id) {
        return SuccessTip.create(userIdentityService.deleteMaster(id));
    }

    @GetMapping
    public Tip queryUserIdentitys(Page<UserIdentity> page,
                                  @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                  @RequestParam(name = "id", required = false) Long id,
                                  @RequestParam(name = "userId", required = false) Long userId,
                                  @RequestParam(name = "identity", required = false) String identity,
                                  @RequestParam(name = "note", required = false) String note,
                                  @RequestParam(name = "orderBy", required = false) String orderBy,
                                  @RequestParam(name = "sort", required = false) String sort) {
        if (orderBy != null && orderBy.length() > 0) {
            if (sort != null && sort.length() > 0) {
                String pattren = "(ASC|DESC|asc|desc)";
                if (!sort.matches(pattren)) {
                    throw new RuntimeException("sort must be ASC or DESC");//此处异常类型根据实际情况而定
                }
            } else {
                sort = "ASC";
            }
            orderBy = "`" + orderBy + "`" + " " + sort;
        }
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        UserIdentity useridentity = new UserIdentity();
        useridentity.setId(id);
        useridentity.setUserId(userId);
        useridentity.setIdentity(identity);
        useridentity.setNote(note);

        page.setRecords(queryUserIdentityService.findUserIdentityPage(page, useridentity, orderBy));

        return SuccessTip.create(page);
    }
}
